const http=require('http');
const fs=require('fs');
const path=require('path');

var filePath=path.join(__dirname,'/data.json');

http.createServer((req,res)=>{
    var data='';
    var reference=fs.readFileSync(filePath);
    reference=JSON.parse(reference.toString());
    req.on('data',(chunk)=>{
        data+=chunk;
    })
    req.on('end',()=>{
        data=JSON.parse(data.toString());
        var userName=data.username;
        var passWord=data.password;
        var flag=false;
        for(var i=0;i<reference.length;i++){
            if(userName==reference[i].username && passWord==reference[i].password){
                console.log('登陆成功');
                flag=true;
            }
        }
        if(!flag){
            console.log('用户名、密码不正确');
        }
    })
}).listen(8081);

console.log('server is listening 8081');